from datetime import datetime
from app.db import db
from app.Pojo.BThing import BThing

#  分类体系表: b_classification
class BClassification(db.Model):
    __tablename__ = 'b_classification'

    pid = db.Column(db.Integer, comment='父级分类ID (0表示一级分类,实现多级分类)')
    title = db.Column(db.String(100), nullable=False, comment='分类名称(如"电子产品""图书")')
    create_time = db.Column(db.DateTime(6), nullable=False, default=datetime.utcnow, comment='分类创建时间')
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='分类唯一主键ID')

    # 一对多关系：一个分类下有多件物品
    # things = db.relationship('BThing', backref='classification', lazy=True)

    def to_dict(self):
        return {
            'id': self.id,
            'pid': self.pid,
            'title': self.title,
            'create_time': self.create_time.isoformat()
        }

    def get_related_things(self):
        from app.Pojo.BThing import BThing
        return BThing.query.filter_by(classification_id=self.id).all()
